import 'package:flutter/material.dart';
import 'package:flutter_house_manager/api/home.dart';
import 'package:flutter_house_manager/utils/toast.dart';
import 'package:flutter_html/flutter_html.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  Map _announceDetail = {};

  /// 获取公告详情
  getAnnounceDetail(String id) async {
    try {
      final res = await getAnnounceDetailAPI(id);
      // PromptAction.showSuccess(res['creatorName']);
      setState(() {
        _announceDetail = res;
      });
    } catch (e) {
      PromptAction.showError(e.toString());
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 从路由参数中获取id
    final id = ModalRoute.of(context)?.settings.arguments as String?;
    // 当有id的时候 才给获取详情数据接口传递id
    if (id != null) {
      getAnnounceDetail(id);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("通知详情"),
      ),
      body: getBody(),
    );
  }

  Widget getBody() {
    if (_announceDetail.isEmpty) {
      return const Center(
        child: CircularProgressIndicator(),
      );
    }
    return ListView(
      scrollDirection: Axis.vertical,
      children: [
        Container(
            padding: const EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(_announceDetail["title"] ?? "",
                    style: const TextStyle(
                        fontWeight: FontWeight.bold, fontSize: 20)),
                const SizedBox(height: 10),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      _announceDetail["creatorName"] ?? "",
                      style: const TextStyle(color: Colors.grey, fontSize: 14),
                    ),
                    Text(
                      _announceDetail["createdAt"] ?? "",
                      style: const TextStyle(color: Colors.grey, fontSize: 14),
                    )
                  ],
                ),
                const SizedBox(height: 20),
                // 富文本组件
                Html(data: _announceDetail["content"] ?? "")
              ],
            ))
      ],
    );
  }
}
